package com.appointmentmanager.utility.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.appointmentmanager.utility.EmailUtility;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class SimpleEmailUtility implements EmailUtility {

	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	private MailSender mailSender;
//	private Logger logger = LoggerFactory.getLogger(getClass());
	 
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void sendMail(String from, String to, String subject, String msg) {
		try {
			SimpleMailMessage message = new SimpleMailMessage();
			
			message.setFrom(from);
			message.setTo(to);
			message.setSubject(subject);
			message.setText(msg);
			mailSender.send(message);
		} catch (MailException e) {
//			logger.debug("Sending mail failed: Invalid e-mail address");
			System.out.println("Sending mail failed: Invalid e-mail address");
		}
	}

	public boolean validate(String email) {
		Pattern pattern = Pattern.compile(EMAIL_PATTERN);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}
}
